/*
 * IndexProperties.java
 *
 * Created on 31 March 2007, 2:47
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package fasea.model;

import fasea.constants.Constants;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.OverlappingFileLockException;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gvasil
 * @author lars-wolfram
 */
//TODO: Refaktoring und Tests schreiben!!!
class IndexProperties {

    private Properties properties;
    private File propFile;

    /**
     * Creates a new instance of IndexProperties
     */
    IndexProperties(File propFile) throws OverlappingFileLockException {
        FileInputStream f = null;
        try {
            this.propFile = propFile;
            if (propFile.length() > 0) {
                properties = new Properties();
                f = new FileInputStream(propFile);
                properties.load(f);
            } else {
                properties = createDefaultPropertyValues();
                save();
            }
        } catch (IOException ex) {
            Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException ex) {
                    Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private Properties createDefaultPropertyValues() {
        Properties props = new Properties();
        props.setProperty("indexing_folders", System.getProperty("user.home"));
        props.setProperty("indexing_folders_file_restrictions", "[]");
        props.setProperty("index_folder", Constants.getDefaultIndexDirectoryPath());
        props.setProperty("last_indexed", "0");
        props.setProperty("indexed_documents_count", "0");
        props.setProperty("version", Constants.getApplicationVersion());
        return props;
    }

    File getFile() {
        return this.propFile;
    }

    synchronized boolean isOldVersion() {
        return !getVersion().equals(Constants.getApplicationVersion());
    }

    final synchronized void save() {
        FileOutputStream s = null;
        try {

            s = new FileOutputStream(this.propFile);
            properties.store(s, "Automatically generated. DO NOT EDIT!!!");
        } catch (Exception ex) {
            Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException ex) {
                    Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    synchronized String getVersion() {
        return this.properties.getProperty("version");
    }

    synchronized long getLastIndexed() {
        return Long.valueOf(this.properties.getProperty("last_indexed"));
    }

    synchronized Date getLastIndexedDate() {
        long milis = Long.valueOf(this.properties.getProperty("last_indexed"));
        return new Date(milis);
    }

    synchronized void setLastIndexed(long millis) {
        this.properties.setProperty("last_indexed", Long.toString(millis));
    }

    synchronized int getIndexedDocumentsCount() {
        String str = this.properties.getProperty("indexed_documents_count");

        if (str == null) {
            this.setIndexedDocumentsCount(0);
            return 0;
        } else {
            return Integer.valueOf(str);
        }
    }

    synchronized void setIndexedDocumentsCount(int n) {
        this.properties.setProperty("indexed_documents_count", Integer.toString(n));
    }

    synchronized File getIndexFolder() {
        String path = this.properties.getProperty("index_folder");
        return new File(path);
    }

    synchronized void setIndexFolder(File directory) {
        try {
            this.properties.setProperty("index_folder", directory.getCanonicalPath());
        } catch (IOException ex) {
            Logger.getLogger(IndexProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    synchronized IndexingFolderList getIndexingFolders() {
        String indexing_folders = this.properties.getProperty("indexing_folders");
        String indexing_folders_file_restrictions = this.properties.getProperty("indexing_folders_file_restrictions");

        IndexingFolderList indexFolderList = new IndexingFolderList(indexing_folders, indexing_folders_file_restrictions);

        return indexFolderList;
    }

    synchronized void removeAllIndexingFolders() {
        this.properties.setProperty("indexing_folders", "");
        this.properties.setProperty("indexing_folders_file_restrictions", "[]");
    }

    synchronized void addIndexingFolder(IndexingFolder directory) {
        String indexing_folders = this.properties.getProperty("indexing_folders");
        String indexing_folders_file_restrictions = this.properties.getProperty("indexing_folders_file_restrictions");

        IndexingFolderList indexFolderList = new IndexingFolderList(indexing_folders, indexing_folders_file_restrictions);
        indexFolderList.add(directory);
        this.properties.setProperty("indexing_folders", indexFolderList.getIndexingFoldersString());
        this.properties.setProperty("indexing_folders_file_restrictions", indexFolderList.getIndexingFolderFileRestrictionsString());
    }
}
